package com.zhs.javacc.projectTwo;

import java.util.Arrays;

import com.zhs.javacc.SimpleNode;
import com.zhs.javacc.XYZParserTreeConstants;

public class WeightCalculator {
	int methodDecNum = 0;
	int assignmentDecNum = 0;
	int arrayAssignmentDecNum = 0;
	int printStatementNum = 0;

	public int getMethodDecNum() {
		return methodDecNum;
	}

	public int getAssignmentStatementNum() {
		return assignmentDecNum;
	}

	public int getArrayAssignmentStatementNum() {
		return arrayAssignmentDecNum;
	}

	public int getPrintStatementNum() {
		return printStatementNum;
	}

	public void countStatementNum(SimpleNode node) {
		int id = Arrays.asList(XYZParserTreeConstants.jjtNodeName).indexOf(
				node.toString());
		switch (id) {
		case XYZParserTreeConstants.JJTPRINTSTATEMENT:
			printStatementNum++;
			break;
		case XYZParserTreeConstants.JJTMETHODDECLARATION:
			methodDecNum++;
			break;
		case XYZParserTreeConstants.JJTASSIGNMENTSTATEMENT:
			assignmentDecNum++;
			break;
		case XYZParserTreeConstants.JJTARRAYASSIGNMENTSTATEMENT:
			arrayAssignmentDecNum++;
			break;
		}

		if (node.jjtGetNumChildren() > 0)
			for (int i = 0; i < node.jjtGetNumChildren(); i++) {
				countStatementNum((SimpleNode) node.jjtGetChild(i));
			}

	}

	public static long calcWeight(SimpleNode node) {
		long weight = 0;
		long factor = 0;

		/*
		 * Count the number of simple statements in the program
		 * System.out.println ( Exp ) ; id = Exp ; id [ Exp ]= Exp ; return Exp;
		 */

		int id = Arrays.asList(XYZParserTreeConstants.jjtNodeName).indexOf(
				node.toString());
		switch (id) {

		case XYZParserTreeConstants.JJTMAINCLASS:
			factor = 1;
			break;
		case XYZParserTreeConstants.JJTTYPEDECLARATION:
			factor = 1;
			break;
		case XYZParserTreeConstants.JJTCLASSDECLARATION:
			factor = 1;
			break;
		case XYZParserTreeConstants.JJTCLASSEXTENDSDECLARATION:
			factor = 1;
			break;

		case XYZParserTreeConstants.JJTMETHODDECLARATION:
			factor = 1;
			break;
		case XYZParserTreeConstants.JJTSTATEMENT:
			factor = 1;
			break;
		case XYZParserTreeConstants.JJTBLOCK:
			factor = 1;
			break;
		case XYZParserTreeConstants.JJTASSIGNMENTSTATEMENT:
			factor = 1;
			break;
		case XYZParserTreeConstants.JJTARRAYASSIGNMENTSTATEMENT:
			factor = 1;
			break;
		case XYZParserTreeConstants.JJTIFSTATEMENT:
			factor = 2;
			break;
		case XYZParserTreeConstants.JJTWHILESTATEMENT:
			factor = 4;
			break;
		case XYZParserTreeConstants.JJTPRINTSTATEMENT:
			factor = 1;
			break;
		case XYZParserTreeConstants.JJTDOWHILESTATEMENT:
			factor = 4;
			break;
		}
		if (node.jjtGetNumChildren() > 0)
			for (int i = 0; i < node.jjtGetNumChildren(); i++) {
				weight += factor
						* (calcWeight((SimpleNode) node.jjtGetChild(i)));
			}
		else {
			weight += factor;
		}
		if (factor > 0 && weight == 0)
			weight = factor;
		node.jjtSetValue(new Long(weight));
		return weight;
	}
}
